'use strict';
var pomelo = require('pomelo');
var webconnector = require('pomelo-webconnector-plugin2');
var status = require('pomelo-status-plugin');
var globalChannel = require('pomelo-globalchannel-plugin');
var analyze = require('kc-analyze');
var onlineUserCountService = require('./app/services/onlineUserCountService.js');

/**
 * Init app for client.
 */ygghhhggh
var app = pomelo.createApp();
app.set('name', 'kingdom crash');

// app configure
app.configure('all', function () {
	var configUtil = require('./app/util/configUtil');
	var status_config = configUtil.load('status');
	var global_channel_config = configUtil.load('globalChannel');
	var analyze_config = configUtil.load('analyze');
	var game_config = configUtil.load('game');

	// 初始化统计模块
	analyze.initialize({
		host: analyze_config.host,
		verify_key: analyze_config.verify_key,
		server_id: game_config.server_id
	});

	app.use(globalChannel, {
		globalChannel: global_channel_config
	});

	app.use(status, {
		status: status_config
	});

	// filter configures
	app.filter(pomelo.timeout());
});

app.configure('all', 'connector', function () {
	app.set('connectorConfig', {
		connector: pomelo.connectors.hybridconnector,
		heartbeat: 15,
		useDict: true,
		useProtobuf: true,
		useCrypto: true
	});

	app.set('proxyConfig', {
		failMode: 'failfast'
	});
});

app.configure('all', 'pay', function () {
	app.set('connectorConfig', {
		connector: webconnector.webconnector,
		methods: 'all',
		useSSL: false
	});
	var webFilter = require('./app/servers/web/filter/webFilter');
	app.filter(webFilter());
});

app.configure('all', 'web', function () {
	app.set('connectorConfig', {
		connector: webconnector.webconnector,
		methods: 'all',
		useSSL: false
	});

	var webFilter = require('./app/servers/web/filter/webFilter');
	app.filter(webFilter());

	require('./app/libs/schedule.js');
});

// handler 热更新开关
// app.set('serverConfig', {
// 	reloadHandlers: true
// });

// remote 热更新开关
// app.set('remoteConfig', {
// 	reloadRemotes: true
// });

// start app
app.start(function () {
    onlineUserCountService.clearOnlineInfo(function() {
    });
});

process.on('uncaughtException', function (err) {
	console.error(' Caught exception: ' + err.stack);
});
